package l1j.william;

import java.util.HashMap;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.L1Inventory;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SystemMessage;

public class RewardModel {

	class RewardTemp {
		private final int[] _itemid;
		private final int[] _count;
		private final int[] _enchantlv;

		private RewardTemp(final int[] itemlist, final int[] countlist,
				final int[] enchantlist) {
			this._itemid = itemlist;
			this._count = countlist;
			this._enchantlv = enchantlist;
		}

		private L1ItemInstance[] getItem(L1PcInstance pc) {
			final int size = this._itemid.length;
			final L1ItemInstance[] items = new L1ItemInstance[size];
			for (int i = 0; i < size; i++) {
				final L1ItemInstance item = ItemTable.getInstance().createItem(
						this._itemid[i]);
				if (item != null) {
					item.setCount(this._count[i]);
					if (item.getItem().getType2() != 0) {
						item.setEnchantLevel(this._enchantlv[i]);
					}
					if (pc.getInventory().checkAddItem(item, this._count[i]) == L1Inventory.OK) {
						pc.getInventory().storeItem(item);
					} else { // 持てない場合は地面に落とす 處理のキャンセルはしない（不正防止）
						L1World.getInstance()
								.getInventory(pc.getX(), pc.getY(),
										pc.getMapId()).storeItem(item);
					}
					items[i] = item;
					pc.sendPackets(new S_ServerMessage(403, item.getLogName())); // %0を手に入れました。
				}
			}
			return items;
		}
	}

	/*
	 * final L1ItemInstance item = ItemTable.getInstance().createItem(item_id);
	 * if (item != null) { item.setCount(count); if
	 * (pc.getInventory().checkAddItem(item, count) == L1Inventory.OK) {
	 * pc.getInventory().storeItem(item); } else { // 持てない場合は地面に落とす
	 * 處理のキャンセルはしない（不正防止） L1World.getInstance().getInventory(pc.getX(),
	 * pc.getY(), pc.getMapId()).storeItem(item); } pc.sendPackets(new
	 * S_ServerMessage(403, item.getLogName())); // %0を手に入れました。 return item; }
	 * return null;
	 */
	private int _quest_id;
	private int _quest_step;

	private String _message;

	private final HashMap<Integer, RewardTemp> _itemList = new HashMap<Integer, RewardTemp>();

	public void addItemList(final int type, final int[] itemlist,
			final int[] countlist, final int[] enchantlist) {
		if (this._itemList.containsKey(type)) {
			System.out
					.println("警告:" + this.getClass().getName() + " 重複加載等級獎勵。");
			return;
		}
		this._itemList.put(type, new RewardTemp(itemlist, countlist,
				enchantlist));
	}

	public void getItemList(L1PcInstance pc) {
		if (pc.getQuest().get_step(this._quest_id) == this._quest_step) {
			return;
		}
		final int type = pc.getType();
		if (this._itemList.containsKey(type)) {
			this._itemList.get(type).getItem(pc);
			pc.sendPackets(new S_SystemMessage(this._message));// TODO 訊息
			pc.getQuest().set_step(this._quest_id, this._quest_step);
		}
	}

	public int getQuestId() {
		return this._quest_id;
	}

	public int getQuestStep() {
		return this._quest_step;
	}

	public void setMsg(final String s) {
		this._message = s;
	}

	public void setQuestId(final int i) {
		this._quest_id = i;
	}

	public void setQuestStep(final int i) {
		this._quest_step = i;
	}
}
